//
//  AnswerViewController.m
//  Japanese+
//
//  Created by Jame on 15/10/24.
//  Copyright © 2015年 SSIC. All rights reserved.
//

#import "AnswerViewController.h"
#import "AusleseCell.h"
#import "CardCell.h"
#import "CircleModel.h"

#import "CardDetailViewController.h"
#import "CreatCardViewController.h"

@interface AnswerViewController () <XLCycleScrollViewDatasource,XLCycleScrollViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate,AusleseCellDelegate>

@end

@implementation AnswerViewController
{
//    VOSegmentedControl *_segment;
    
//    UIView *_ausleseView;
//    XLCycleScrollView *_photoView;
//    UICollectionView *_ausleseCollection;
    TeacherModel *_teacherModel;
    MemberModel *_memberModel;
    
    NSMutableArray *_dataArray;
    UIView *_cardView;
    UICollectionView *_cardCollection;
    CircleModel *_circleModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatNavigationBarWithImage:nil title:nil];
    [self creatNavigationBar];
    [self creatNavigationBarLeftItemWithLeftTitle:nil LeftImage:ECIMAGENAME(@"back_button_B.png")];
    [self creatNavigationBarRightItemWithRightTitle:nil RightImage:ECIMAGENAME(@"right_icon.png")];

    self.leftLabel.textColor = ECCOLOR(0, 0, 0, 1);
    self.leftImageView.frame = CGRectMake(10, 30, 25, 25);
    self.rightLabel.textColor = ECCOLOR(0, 0, 0, 1);
    self.rightImageView.frame = CGRectMake(ECSCREEN_W - 33, 30, 25, 25);
    
    [self creatCardView];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self loadUserInfo];
    [self creatHttpRequest];
}


- (void)loadUserInfo
{
    UserInfoManager *userInfo = [UserInfoManager sharedUserInfoManager];
    [userInfo loadUserInfoFromSanbox];
    _memberModel = userInfo.memberModel;
    _teacherModel = userInfo.teacherModel;
}


- (void)leftBtnClick:(id)leftSender
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)rightBtnClick:(id)rightSender
{
    CreatCardViewController *vc = [[CreatCardViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)creatHttpRequest
{
    NSDictionary *param = @{
                            @"pageIndex":@"0",
                            @"pageSize":@"999",
                            };
    [[HttpManager defaultManager] postRequestToUrl:RequestURL(CIRCLE) params:param complete:^(BOOL successed, NSDictionary *result) {
        if (successed) {
            [CircleModel setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"data":@"CircleData",
                         };
            }];
            
            if (_dataArray) {
                [_dataArray removeAllObjects];
            }else{
                _dataArray = [[NSMutableArray alloc] init];
            }
            
            _circleModel = [[CircleModel alloc] init];
            _circleModel = [CircleModel objectWithKeyValues:result];
            [_dataArray addObjectsFromArray:_circleModel.data];
            [_cardCollection reloadData];
        }
    }];
}

- (void)creatNavigationBar
{
    CGRect rect = [[UIApplication sharedApplication] statusBarFrame];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, rect.size.height, ECSCREEN_W, 64-rect.size.height)];
    view.backgroundColor = ECCOLOR(255, 255, 255, 1);
    [self.navcImageView addSubview:view];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 64-rect.size.height)];
    label.text = @"帖子";
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:18.f];
    [view addSubview:label];
    __weak typeof (self) weakSelf = self;
    __weak typeof (_cardView) weakCard = _cardView;
//    __weak typeof (_ausleseView) weakAuslese = _ausleseView;

    
//    _segment = [[VOSegmentedControl alloc] initWithSegments:@[@{
//                                                                 @"text":@"精选"
//                                                                 },@{
//                                                                 @"text":@"帖子"
//                                                                 }]];
//    _segment.frame = CGRectMake(30, 0, ECSCREEN_W-60, 64-rect.size.height);
//    _segment.animationType = VOSegCtrlAnimationTypeSmooth;
//    _segment.contentStyle = VOContentStyleTextAlone;
//    _segment.indicatorStyle = VOSegCtrlIndicatorStyleBottomLine;
//    _segment.allowNoSelection = NO;
//    _segment.scrollBounce = NO;
//    _segment.selectedTextFont = [UIFont systemFontOfSize:15];
//    _segment.selectedIndicatorColor = NAVC_COLOR;
//    _segment.textColor = ECCOLOR(0, 0, 0, 1);
//    _segment.selectedTextColor = NAVC_COLOR;
//    _segment.selectedBackgroundColor = ECCOLOR(255, 255, 255, 1);
//    _segment.backgroundColor = [UIColor clearColor];
//    _segment.indicatorThickness = 2;
//    _segment.indicatorCornerRadius = 15;
//    [_segment addTarget:self action:@selector(segmentClick:) forControlEvents:UIControlEventValueChanged];
//    [view addSubview:_segment];
//    [self segmentClick:_segment];
}


//- (void)segmentClick:(VOSegmentedControl *)segment
//{
//    switch (segment.selectedSegmentIndex) {
//        case 0:
//        {
//            if (_cardView) {
//                [_cardView removeFromSuperview];
//            }
//            [self creatAusleseView];
//
//        }
//            break;
//        case 1:
//        {
//            if (_ausleseView) {
//                [_ausleseView removeFromSuperview];
//            }
//            [self creatCardView];
//        }
//            break;
//            
//        default:
//            break;
//    }
//}


//- (void)creatAusleseView
//{
//    _ausleseView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, ECSCREEN_H - 64)];
//    [self.view addSubview:_ausleseView];
//    
//    
//    _photoView = [[XLCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, 150) withOutTimer:NO];
//    _photoView.delegate = self;
//    _photoView.datasource = self;
//    _photoView.pageControl.hidden = NO;
//    [_ausleseView addSubview:_photoView];
//    
//    
//    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
//    
//    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
//    CGFloat w = (ECSCREEN_W - 50)/2;
//    layout.itemSize = CGSizeMake(w, w + 40);
//    layout.minimumInteritemSpacing = 10;
//    layout.minimumLineSpacing = 10;
//    layout.sectionInset =  UIEdgeInsetsMake(20, 20, 20, 20);
//
//    _ausleseCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 150, ECSCREEN_W, ECSCREEN_H - 64 - 150) collectionViewLayout:layout];
//    _ausleseCollection.backgroundColor = ECCOLOR(255, 255, 255, 1);
//    
//    _ausleseCollection.delegate = self;
//    _ausleseCollection.dataSource = self;
//    
//    [_ausleseCollection registerNib:[UINib nibWithNibName:@"AusleseCell" bundle:nil] forCellWithReuseIdentifier:@"ausleseCell"];
//    
//    [_ausleseView addSubview:_ausleseCollection];
//}
//
//- (NSInteger)numberOfPages
//{
//    return 3;
//}
//
//
//- (UIView *)pageAtIndex:(NSInteger)index
//{
//    UIImageView *imageView = [[UIImageView alloc]initWithFrame:_photoView.frame];
//    imageView.backgroundColor   = SECOND_COLOR;
//    imageView.image = ECIMAGENAME(@"main_photo.png");
//    return imageView;
//}



#pragma mark - numOfSections
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


#pragma mark - numOfItems
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataArray.count + 1;
}


#pragma mark - cellForItemIndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if (collectionView == _ausleseCollection) {
//        static NSString *reuseIdentifier = @"ausleseCell";
//        AusleseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
//        [cell setCellWithModel:nil WithIndexPath:indexPath];
//        cell.delegate = self;
//        return cell;
//    }else{
        static NSString *reuseIdentifier = @"cardCell";
        CardCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
        cell.clipsToBounds = YES;
        cell.layer.cornerRadius = 3;
        cell.backgroundColor = ECCOLOR(255, 255, 255, 1);
        [cell setCellWithModel:_dataArray WithIndexPath:indexPath];
        return cell;
//    }
}


//- (void)ausleseCell:(AusleseCell *)cell listenClick:(UIButton *)listen
//{
//    NSIndexPath *indexPath = [_ausleseCollection indexPathForCell:cell];
//    NSLog(@"----%ld----收听",(long)indexPath.row);
//}
//
//
//- (void)ausleseCell:(AusleseCell *)cell playClick:(UIButton *)play
//{
//    NSIndexPath *indexPath = [_ausleseCollection indexPathForCell:cell];
//    NSLog(@"----%ld----播放",(long)indexPath.row);
//}


- (void)creatCardView
{
    _cardView = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ECSCREEN_W, ECSCREEN_H - 64)];
    [self.view addSubview:_cardView];
    
    
//    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, ECSCREEN_W - 20, 40)];
//    label.text = @"我的圈子";
//    label.font = [UIFont systemFontOfSize:13.f];
//    [_cardView addSubview:label];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat w = (ECSCREEN_W - 30)/2;
    layout.itemSize = CGSizeMake(w, w - 20);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset =  UIEdgeInsetsMake(10, 10, 10, 10);
    
    _cardCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ECSCREEN_W, ECSCREEN_H - 64) collectionViewLayout:layout];
    _cardCollection.backgroundColor = BG_COLOR;
    
    _cardCollection.delegate = self;
    _cardCollection.dataSource = self;
    
    [_cardCollection registerNib:[UINib nibWithNibName:@"CardCell" bundle:nil] forCellWithReuseIdentifier:@"cardCell"];
    
    [_cardView addSubview:_cardCollection];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _cardCollection) {
        if (indexPath.row >= _dataArray.count) {
            NSLog(@"-----添加圈子-----");
        }else{
            CardDetailViewController *vc = [[CardDetailViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
